package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.domain.Outward;
import com.tc.dto.OutwardDTO;
import com.tc.mapper.OutwardMapper;
import com.tc.service.OutwardService;
import com.tc.utils.ResultVo;
import com.tc.vo.OutwardVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class OutwardServiceImpl extends ServiceImpl<OutwardMapper, Outward> implements OutwardService {
    @Resource
    private OutwardMapper outwardMapper;
    @Transactional(rollbackFor = Exception.class)
    /**
     * 护工申请
     */
    @Override
    public ResultVo examineOutward(Outward outward) throws Exception {
        // 检查审批状态是否为null
        if(outward.getAuditStatus() == null) {
            return ResultVo.fail("审批状态不能为空");
        }
        
        // 创建只包含需要更新字段的对象
        Outward updateOutward = new Outward();
        updateOutward.setId(outward.getId());
        updateOutward.setAuditStatus(outward.getAuditStatus());
        updateOutward.setAuditPerson(outward.getAuditPerson());
        updateOutward.setAuditTime(outward.getAuditTime());
        
        // 使用UpdateWrapper指定更新条件
        UpdateWrapper<Outward> updateWrapper = new UpdateWrapper<Outward>();
        updateWrapper.eq("id", outward.getId());
        
        // 执行更新操作
        boolean success = update(updateOutward, updateWrapper);
        
        if(success) {
            return ResultVo.ok("审批成功");
        } else {
            return ResultVo.fail("审批失败，请稍后重试");
        }
    }


    /**
     * 根据客户找护工
     *
     * @param outwardDTO
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo<Page<OutwardVo>> queryOutwardVo(OutwardDTO outwardDTO) throws Exception {
        Page<OutwardVo> page = new Page<>(outwardDTO.getPageSize(), 6);
        outwardMapper.selectOutwardVo(page, outwardDTO.getUserId());
        return ResultVo.ok(page);
    }



}
